/**
Nectarine Utility Project is a software interface for the web system 'demovibes'
Copyright (C) 2010  Alexandre LAURENT

This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.

website: http://code.google.com/p/nectarine-utility-project/
e-mail: lw.demoscene@gmail.com
**/

#include "queuewidget.h"

#include <QFrame>
#include <QVBoxLayout>
#include <QGroupBox>
#include <QBoxLayout>
#include <QHeaderView>
#include <QList>
#include <QLabel>

#include <QDebug>

#include "songrequestwidget.h"
#include "songlistwidget.h"

QueueWidget :: QueueWidget(QWidget *parent)
    :QWidget(parent)
{
    pVerticalBox = new QVBoxLayout(this);

    // Creation of the interface for the playing song
    pSongRequestWidget = new SongRequestWidget(this);
    pSongRequestWidget->setSizePolicy(QSizePolicy(QSizePolicy::Minimum,QSizePolicy::Maximum));

    // Creation of the interface for the queue
    pQueueGBox = new QGroupBox(tr("Queue"));
    pQueueGBox->setAlignment(Qt::AlignHCenter);
    pQueueLayout = new QBoxLayout(QBoxLayout::TopToBottom,pQueueGBox);

    pQueueGBox->setLayout(pQueueLayout);

    pQueueList = new SongListWidget(pQueueGBox);
    pQueueList->setSizePolicy(QSizePolicy(QSizePolicy::Minimum,QSizePolicy::Maximum));
    pQueueList->verticalHeader()->hide();
    pQueueLayout->addWidget(pQueueList);


    // Creation of the interface for the history
    pHistoryGBox = new QGroupBox(tr("History"));
    pHistoryGBox->setAlignment(Qt::AlignHCenter);
    pHistoryLayout = new QBoxLayout(QBoxLayout::TopToBottom,pHistoryGBox);

    pHistoryGBox->setLayout(pHistoryLayout);

    pHistoryList = new SongListWidget(pHistoryGBox);
    pHistoryList->setSizePolicy(QSizePolicy(QSizePolicy::Minimum,QSizePolicy::Maximum));
    pHistoryList->verticalHeader()->hide();
    pHistoryLayout->addWidget(pHistoryList);

    // Adding the QGroupBox to the QVBoxLayout
    pVerticalBox->addWidget(pSongRequestWidget);
    pVerticalBox->addWidget(pQueueGBox);
    pVerticalBox->addWidget(pHistoryGBox);

    qDebug() << "QueueWidget created";
}

QueueWidget :: ~QueueWidget(void)
{
    qDebug() << "QueueWidget deleted";
}

void QueueWidget :: fillPlayingSong(void)
{
    qDebug() << "QueueWidget :: fillPlayingSong";

    pSongRequestWidget->setData(this->playingSong);
}

void QueueWidget :: fillQueue(void)
{
    qDebug() << "QueueWidget :: fillQueue";

    pQueueList->setData(this->queueData);
}

void QueueWidget :: fillHistory(void)
{
    qDebug() << "QueueWidget :: fillHistory";

    pHistoryList->setData(this->historyData);
}

void QueueWidget :: setData(const SongRequest& playingSong, const QList<SongRequest>& queueData, const QList<SongRequest>& historyData)
{
    this->setPlayingSong(playingSong);
    this->setQueueData(queueData);
    this->setHistoryData(historyData);
}

void QueueWidget :: setPlayingSong(const SongRequest& playingSong)
{
    qDebug() << "QueueWidget :: setPlayingSong";

    this->playingSong = playingSong;

    // Refill the interface
    this->fillPlayingSong();
}

void QueueWidget :: setQueueData(const QList<SongRequest>& queueData)
{
    qDebug() << "QueueWidget :: queueData";

    this->queueData = queueData;

    // Refill the interface
    this->fillQueue();
}

void QueueWidget :: setHistoryData(const QList<SongRequest>& historyData)
{
    qDebug() << "QueueWidget :: historyData";

    this->historyData = historyData;

    // Refill the interface
    this->fillHistory();
}

unsigned int QueueWidget :: getUserRequestCount(const QString& username)const
{
    unsigned int requestCounter = 0;

    qDebug() << "QueueWidget :: getUserRequestCount (" << username << ")";

    for ( QList<SongRequest>::const_iterator itSongRequest = queueData.begin() ; itSongRequest != queueData.end() ; ++itSongRequest )
    {
        if ( itSongRequest->requesterInfo.name == username )
        {
            requestCounter++;
        }
    }

    qDebug() << "\t\tCount: " << requestCounter;

    return requestCounter;
}

void QueueWidget :: setConnection(const bool isConnected)
{
    qDebug() << "QueueWidget :: setConnection(" << isConnected << ")";

    this->pSongRequestWidget->setConnected(isConnected);
}
